package com.leesche.zcsp.node.net.service.impl;

import com.leesche.zcsp.node.net.service.NetService;
import com.leesche.zcsp.node.net.service.RepeatSendService;
import com.leesche.zcsp.node.net.service.data.TimerData;
import com.leesche.zcsp.node.utils.PayConfig;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class RepeatSendServiceImpl implements RepeatSendService{

	private Map<String, TimerData> timerDataList = new HashMap<String, TimerData>();
	private Thread timerThread;
	private boolean isTimerStop = false;
	private long currentTime = 0;

/*	@Autowired
	private NetService netService;*/

	private static final Logger LOGGER = Logger
			.getLogger(RepeatSendServiceImpl.class);

	public void start() {
		currentTime = System.currentTimeMillis();

		timerThread = new Thread() {
			@Override
			public void run() {
				LOGGER.info("RepeatSendServiceImpl......");

				while (!isTimerStop) {

					List<String> pastList = new ArrayList<String>();
					synchronized (this) {
						Iterator<String> data = timerDataList.keySet()
								.iterator();
						long deltime = System.currentTimeMillis() - currentTime;
						currentTime = System.currentTimeMillis();
						while (data.hasNext()) {
							String key = data.next();
							TimerData timerData = timerDataList.get(key);
							timerData.pastTime += deltime;
							if (timerData.pastTime > PayConfig.OVERTIME) {
								pastList.add(key);
							}

						}
						for (int i = 0; i < pastList.size(); ++i) {
							String deviceName = pastList.get(i);
							TimerData timerData = timerDataList.get(deviceName);
							if (timerData.sendTimes >= 3)
							{
								timerDataList.remove(deviceName);
							}
							else
							{
								addDevice(timerData.deviceName, timerData.deviceType, timerData.money, timerData.coin, timerData.payType);
							}
						}

					}

					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						LOGGER.info("sleep error");
					}
				}
			}
		};
		timerThread.start();
	}

	public void addDevice(String deviceName, int deviceType, int money, int coin, int payType) {
		TimerData timerData = timerDataList.get(deviceName);
		if (timerData == null) {
			timerData = new TimerData();
			
			timerData.sendTimes = 0;
		}

		timerData.deviceName = deviceName;
		timerData.deviceType = deviceType;
		timerData.money = money;
		timerData.coin = coin;
		timerData.payType = payType;
		
		timerData.pastTime = 0;
		timerData.sendTimes += 1;
		timerDataList.put(deviceName, timerData);

		LOGGER.debug("RepeatSendServiceImpl.netService.sendClientBuy......");
		//netService.sendClientBuy(deviceName, deviceType, money, coin, payType);
	}

	public void addSynDevice(String deviceName, int deviceType, int money, int coin, int payType)
	{
		synchronized (this) {
			addDevice(deviceName, deviceType, money, coin, payType);
		}
	}
	
	public void delSynDevice(String deviceName) {
		synchronized (this) {
			timerDataList.remove(deviceName);
		}
	}

	public void close() {
		isTimerStop = true;
		timerThread.stop();
	}
}
